package com.ie.controller.admin;


import com.ie.comment.http.ResponseResult;
import com.ie.comment.sercurity.anno.AuthIgrone;
import com.ie.entity.GoodsEntity;
import com.ie.sevice.admin.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author hp
 */
@RestController
@RequestMapping("/api/admin/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    private static byte[] filecontent;
    private static String fileName;

    @AuthIgrone
    @PostMapping("/getGoods")
    public ResponseResult<Map<String , Object>> getGoods(@RequestBody GoodsEntity goodsEntity){

        System.out.println("getGoods调用成功");
        return goodsService.getGoods(goodsEntity);
    }
    //广告商品的获取
    @AuthIgrone
    @PostMapping("/getAdvGoods")
    public ResponseResult<List<GoodsEntity>> getAdvGoods(){
        System.out.println("getAdvGoods调用成功");
        return goodsService.getAdvGoods();
    }

    @AuthIgrone
    @PostMapping("/getGoodsById")
    public ResponseResult<GoodsEntity> getGoodsById(@RequestBody GoodsEntity goodsEntity){
        System.out.println("getGoodsById调用成功");
        return goodsService.getGoodsById(goodsEntity);
    }


    @AuthIgrone
    @PostMapping("/getGoodsIndex")
    public ResponseResult<List<GoodsEntity>> getGoodsIndex(@RequestBody GoodsEntity goodsEntity){
        System.out.println("getGoodsIndex调用成功");
        return goodsService.getGoodsIndex(goodsEntity);
    }


    @AuthIgrone
    @PostMapping("/fileInit")
    public void fileInit(@RequestBody MultipartFile file) {
        // MultipartFile对象不能在另一个方法中使用，所以把文件对象变成字节数组
        try {
            filecontent = file.getBytes();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        fileName = file.getOriginalFilename();
    }

    @AuthIgrone
    @PostMapping("/add")
    public ResponseResult<Map<String , Object>> addGoods(@RequestBody GoodsEntity goodsEntity) {
        goodsEntity.setFileName(fileName);
        goodsEntity.setLogoFile(filecontent);
        filecontent = new byte[0];
        return goodsService.addGoods(goodsEntity);
    }


    @AuthIgrone
    @PostMapping("/delete")
    public ResponseResult<Map<String , Object>> deleteGoods(@RequestBody GoodsEntity goodsEntity) {

        return goodsService.deleteGoods(goodsEntity);
    }
}
